package com.co.vote.web.controller.user;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.co.server.entity.user.TUserActive;
import com.co.server.entity.user.TUsers;
import com.co.server.service.user.TUserActiveService;
import com.co.server.service.user.TUsersService;
import com.co.vote.constant.HttpStatusCode;
import com.co.vote.utils.ClientUtil;

/**
 * 用户注册
 * @author ZYL
 * @date 2016年12月18日 下午10:57:22
 */
@Controller
@RequestMapping("/register")
public class RegisterController {

	@Resource
	private TUsersService tUsersService;
	@Resource
	private TUserActiveService tUserActiveService;
	
	/**
	 * 注册页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/init")
	public String init(Model model) {
		return "user/registerInit";
	}
	
	/**
	 * 注册
	 * @param session 
	 * @param request
	 * @param tUsers   用户对象
	 * @param verifycode 验证码
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/register")
	public Map<String,Object> register(HttpSession session,HttpServletRequest request,TUsers tUsers,String verifycode) {
		//手机号邮箱都应支持注册，但目前只支持邮箱
		Map<String,Object> mapResult = new HashMap<String,Object>();
		final int CODE_ERROR_IDENTITY = 401;
		TUserActive  tUserActive = null ;
		if (StringUtils.isBlank(verifycode) || verifycode.length() != 6) {
			mapResult.put("code",CODE_ERROR_IDENTITY);
			mapResult.put("desc", "验证码错误!");
			return mapResult;
		}else{
			
			tUserActive =  tUserActiveService.queryLastByEmail(tUsers.getUserName());
			if( tUserActive == null){
				mapResult.put("code",CODE_ERROR_IDENTITY);
				mapResult.put("desc", "验证码错误!");
				return mapResult;
			}else{
				if(tUserActive.getVcode().equals(verifycode)){
					//判断是否过期 目前三十分钟过期
					if (System.currentTimeMillis() - tUserActive.getCreateTime().getTime() > 30 * 60 * 1000){
						//验证码过期
						mapResult.put("code",CODE_ERROR_IDENTITY);
						mapResult.put("desc", "验证码过期!");
						return mapResult;
					}
				}else{
					mapResult.put("code",CODE_ERROR_IDENTITY);
					mapResult.put("desc", "验证码错误!");
					return mapResult;
				}
			}
		}
		
		if(tUsersService.queryByLoginName(tUsers.getUserName()) != null){
			mapResult.put("code",HttpStatusCode.CODE_ERROR);
			mapResult.put("desc", "该邮箱已经被注册!");
			return mapResult;
		}
		//新增用户
		tUsersService.addUser(tUsers, tUserActive);
		//用户登录
		mapResult.put("code",HttpStatusCode.CODE_SUCCESS);
		return mapResult;
	}
	
	
	/**
	 * 检查是否有该用户
	 * @param username
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/checkUser")
	public int checkUser(String username){
		TUsers user = tUsersService.queryByLoginName(username);
		if(user != null){
			return HttpStatusCode.CODE_ERROR;
		}else{
			return HttpStatusCode.CODE_SUCCESS;
		}
	}
	
	/**
	 * 发送验证码
	 * @param request
	 * @param userName
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/sendEmailCode")
	public int sendRegisterCode(HttpServletRequest request,String userName){
		String ip = ClientUtil.getIp(request);
		tUserActiveService.addBindCodeByEmail(userName,userName,ip);
		return HttpStatusCode.CODE_SUCCESS;
	}
	
}
